package com.hwj.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.hwj.service.NotifyService;
import com.hwj.utils.JsonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@CrossOrigin
@RestController
@RequestMapping("/notify")
public class NotifyController {
    @Autowired
    private NotifyService notifyService;

    /**
     * 消息未读条数
     * @param request
     * @return
     */
    @GetMapping("/getCount")
    public JsonResponse getCount(HttpServletRequest request){
        JWT jwt = JWTUtil.parseToken(request.getHeader("token"));
        Long id= Convert.toLong(jwt.getPayload("userId"));
        return JsonResponse.successResponse(0,"查询消息未读条数成功",notifyService.getCount(id));
    }

    /**
     * 消息列表
     * @param request
     * @return
     */
    @GetMapping("/list")
    public JsonResponse listNotify(HttpServletRequest request){
        JWT jwt = JWTUtil.parseToken(request.getHeader("token"));
        Long id= Convert.toLong(jwt.getPayload("userId"));
        return JsonResponse.successResponse(0,"查询消息列表成功",notifyService.listNotify(id));
    }
}
